﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;

namespace TaiwuZD
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static RoutedCommand ChooiseFile1 { get; set; } = new RoutedCommand();
        public static RoutedCommand ChooiseFile2 { get; set; } = new RoutedCommand();
        public static RoutedCommand ChooiseFile3 { get; set; } = new RoutedCommand();


        public static RoutedCommand OpenSaveDirectoryCommand { get; set; } = new RoutedCommand();



        public MainWindow()
        {

            InitializeComponent();

            //var d= Global.Inst;
            ImageSourceCache.Init();

            var dd = new TwItemTypes();
            //            GameAssets.LoadAssets();
        }

        #region 选择存档
        private bool ChooseFileCanExecute(int id)
        {
            if (!Global.Inst.IsTaiwuDirectoryFound)
                return false;

            for (int i = 0; i < 9; i++)
            {
                if (!File.Exists(Path.Combine(Global.Inst.TaiwuStoragePath, $"Date_{id}", $"TW_Save_Date_{i}.tw")))
                {
                    return false;
                }
            }
            return true;
        }

        private void ChooseFileExecute(int id)
        {
            Global.Inst.StorageFilePath = Path.Combine(Global.Inst.TaiwuStoragePath, $"Date_{id}");
        }

        private void ChooseFile1CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ChooseFileCanExecute(1);
        }

        private void ChooseFile2CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ChooseFileCanExecute(2);
        }

        private void ChooseFile3CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ChooseFileCanExecute(3);
        }

        private void ChooseFile1_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            ChooseFileExecute(1);
        }

        private void ChooseFile2_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            ChooseFileExecute(2);
        }

        private void ChooseFile3_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            ChooseFileExecute(3);
        }
        #endregion

        private void btnChooseOther_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.ShowNewFolderButton = false;
            folderBrowserDialog.Description = "请选择文件路径";

            folderBrowserDialog.SelectedPath = "C:\\";

            if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Global.Inst.StorageFilePath = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnSaveFile_Click(object sender, RoutedEventArgs e)
        {
            if (!Global.Inst.IsSFLoaded)
                return;

            if (sender is Button)
            (sender as Button).Enabled = false;
            Global.Inst.SaveSaveFile();

            popSaveTip.IsOpen = true;
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.AutoReset = false;
            timer.Interval = 2000;
            timer.Elapsed += Timer_Elapsed;
            timer.Enabled = true;

            if (sender is Button)
                (sender as Button).Enabled = true;
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() => { popSaveTip.IsOpen = false; }));
        }

        private void OpenSaveDirectoryCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = Global.Inst.IsSFLoaded;
        }

        private void OpenSaveDirectoryCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(Global.Inst.StorageFilePath);
        }
    }
}
